﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PIM_ATE.Electrical
{
    public partial class FrmCalConfirm : Form
    {
        public delegate void delgetCalENA(ref float s11, ref float s22, ref float s21);
        public event delgetCalENA evtgetCalENA;
        public ClsCalRecordItem RdItem1, RdItem2, RdItem3;
        public FrmCalConfirm(string port, string band)
        {
            InitializeComponent();
            RdItem1 = new ClsCalRecordItem();
            RdItem2 = new ClsCalRecordItem();
            RdItem3 = new ClsCalRecordItem();
            RdItem1.Port = port;
            RdItem1.PortIndex = "S11";
            RdItem1.Band = band;
            RdItem2.Port = port;
            RdItem2.PortIndex = "S22";
            RdItem2.Band = band;
            RdItem3.Port = port;
            RdItem3.PortIndex = "S21";
            RdItem3.Band = band;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            float s11 = 0f,
                  s22 = 0f,
                  s21 = 0f;
            string s11Spec = "[-INF," + nud_S11Spec.Value.ToString() + "]",
                   s22Spec = "[-INF," + nud_S22Spec.Value.ToString() + "]",
                   s21Spec = "[-INF," + nud_S21Spec.Value.ToString() + "]";
            evtgetCalENA(ref s11, ref s22, ref s21);


            txtS11Val.Text = s11.ToString();
            txtS22Val.Text = s22.ToString();
            txtS21Val.Text = s21.ToString();
            txtS11Val.ForeColor = Color.Black;
            txtS22Val.ForeColor = Color.Black;
            txtS21Val.ForeColor = Color.Black;
            bool bResS11 = PIMFunction.compareSpec(s11,s11Spec);
            if (!bResS11)
            {
                txtS11Val.ForeColor = Color.Red;
            }
            bool bResS22 = PIMFunction.compareSpec(s22,s22Spec);
            if (!bResS22)
            {
                txtS22Val.ForeColor = Color.Red;
            }
            bool bResS21 = PIMFunction.compareSpec(s21, s21Spec);
            if (!bResS21)
            {
                txtS21Val.ForeColor = Color.Red;
            }


            bool bRes = bResS11 && bResS22 && bResS21;
            if (bRes)
            {
                lblRES.Text = GDefine.ContentPass;
                lblRES.ForeColor = Color.Green;
            }
            else
            {
                lblRES.Text = GDefine.ContentFail;
                lblRES.ForeColor = Color.Red;
            }



            RdItem1.Value = s11.ToString();
            RdItem1.Spec = s11Spec.ToString();
            RdItem1.ValueType = "驻波";
            RdItem1.IsPass = lblRES.Text;

            RdItem2.Value = s22.ToString();
            RdItem2.Spec = s22Spec.ToString();
            RdItem2.ValueType = "驻波";
            RdItem2.IsPass = lblRES.Text;

            RdItem3.Value = s21.ToString();
            RdItem3.Spec = s21Spec.ToString();
            RdItem3.ValueType = "隔离";
            RdItem3.IsPass = lblRES.Text;

        }
        bool DealResult(string spec, float value)
        {
            bool ret = false;
            string sResult = "";
            bool bResult = PIMFunction.GetResult(spec, value, 0);
            if (bResult)
            {
                sResult = GDefine.ContentPass;
                ret = true;
            }
            else
            {
                sResult = GDefine.ContentFail;
            }
            return ret;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void btnRetry_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Retry;
        }

        private void FrmCalConfirm_Load(object sender, EventArgs e)
        {
            button1.PerformClick();
            this.ActiveControl = this.btnOK;
        }

        private void txtS11Val_KeyPress(object sender, KeyPressEventArgs e)
        {
            button1.PerformClick();
        }

    }
}
